Introductory example

Info: this guide gives you an overview of the typical optimization workflow with mrMBO. For a much more detailed introduction see the next chapter.

Here we provide a quickstart example for you to make yourself familiar with mlrMBO. We aim to optimize the one dimensional Rastrigin function using model-based optimization. Instead of writing this function by hand, we make use of the smoof package, which offers a lot of common single objective optimization functions.

library(smoof)
library(mlr)
library(mlrMBO)
library(ParamHelpers)
obj.fun = makeRastriginFunction(1)

Note: Since all this stuff here is still under developement it might be neccessary to install the github developement version of the ParamHelpers package via the devtools::install_github function.

We decide to use kriging as our surrogate model and to do 10 sequential optimization steps. Furthermore we use Expected Improvement (EI) as the infill criterion, i. e., the criterion which determines which point(s) of the objective function should be evaluated in each iterations (keep in mind, that using EI as the infill criterion needs the learner to support standard error estimation).

As a last step we have to generate an initial design on which we evaluate our model in the beginning. We use ParamHelpers::generateDesign to generate 10 points in a latin hypercube design.

learner = makeLearner("regr.km", predict.type = "se", covtype = "matern3_2")
control = makeMBOControl()
control = setMBOControlTermination(control, iters = 10)
control = setMBOControlInfill(control, crit = "ei")
design = generateDesign(n = 10, par.set = getParamSet(obj.fun))

Finally we start the optimization process and print the result object.

result = mbo(obj.fun, design = design, learner = learner, control = control, 
             show.info = TRUE)
#> Computing y column(s) for design. Not provided.
#> [mbo] 0: x=4.17 : y = 22.8 : 0.0 secs : initdesign
#> [mbo] 0: x=2.74 : y = 18.4 : 0.0 secs : initdesign
#> [mbo] 0: x=0.272 : y = 11.5 : 0.0 secs : initdesign
#> [mbo] 0: x=-1.65 : y = 18.4 : 0.0 secs : initdesign
#> [mbo] 0: x=2.03 : y = 4.32 : 0.0 secs : initdesign
#> [mbo] 0: x=3.11 : y = 12.1 : 0.0 secs : initdesign
#> [mbo] 0: x=-2.44 : y = 25.3 : 0.0 secs : initdesign
#> [mbo] 0: x=-3.89 : y = 17.4 : 0.0 secs : initdesign
#> [mbo] 0: x=-0.769 : y = 9.37 : 0.0 secs : initdesign
#> [mbo] 0: x=-4.37 : y = 35.9 : 0.0 secs : initdesign
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -42.16228 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       42.162  |proj g|=       1.9768
#> At iterate     1  f =       35.839  |proj g|=       0.31573
#> At iterate     2  f =        35.68  |proj g|=             0
#> 
#> iterations 2
#> function evaluations 3
#> segments explored during Cauchy searches 2
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 35.6801
#> 
#> F = 35.6801
#> final  value 35.680121 
#> converged
#> [mbo] 1: x=-2.05 : y = 4.7 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -50.85536 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       50.855  |proj g|=       1.8606
#> At iterate     1  f =       39.744  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 39.7438
#> 
#> F = 39.7438
#> final  value 39.743841 
#> converged
#> [mbo] 2: x=3.62 : y = 30.3 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -46.05445 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       46.054  |proj g|=      0.45522
#> At iterate     1  f =       43.926  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 43.9265
#> 
#> F = 43.9265
#> final  value 43.926485 
#> converged
#> [mbo] 3: x=-3.55 : y = 32.2 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -49.18026 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=        49.18  |proj g|=      0.30591
#> At iterate     1  f =       48.175  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 48.1752
#> 
#> F = 48.1752
#> final  value 48.175161 
#> converged
#> [mbo] 4: x=3.72 : y = 25.6 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -52.05361 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       52.054  |proj g|=      0.30731
#> At iterate     1  f =       51.605  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 51.6047
#> 
#> F = 51.6047
#> final  value 51.604695 
#> converged
#> [mbo] 5: x=-3.79 : y = 22 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -54.14426 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       54.144  |proj g|=       5.6298
#> At iterate     1  f =       54.011  |proj g|=       0.22682
#> At iterate     2  f =       53.963  |proj g|=        0.1198
#> At iterate     3  f =       53.963  |proj g|=     0.0074116
#> At iterate     4  f =       53.963  |proj g|=     1.392e-05
#> At iterate     5  f =       53.963  |proj g|=    1.5912e-09
#> 
#> iterations 5
#> function evaluations 8
#> segments explored during Cauchy searches 5
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 1.59118e-09
#> final function value 53.9628
#> 
#> F = 53.9628
#> final  value 53.962788 
#> converged
#> [mbo] 6: x=1.94 : y = 4.5 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -57.42694 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       57.427  |proj g|=      0.37626
#> At iterate     1  f =       56.944  |proj g|=       0.31386
#> At iterate     2  f =       56.668  |proj g|=       0.24689
#> At iterate     3  f =       56.657  |proj g|=       0.52416
#> At iterate     4  f =       56.656  |proj g|=      0.025438
#> At iterate     5  f =       56.656  |proj g|=    0.00038393
#> At iterate     6  f =       56.656  |proj g|=    2.9003e-07
#> 
#> iterations 6
#> function evaluations 9
#> segments explored during Cauchy searches 6
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 2.90031e-07
#> final function value 56.6555
#> 
#> F = 56.6555
#> final  value 56.655547 
#> converged
#> [mbo] 7: x=-1.96 : y = 4.13 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -68.84067 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       68.841  |proj g|=       1.4873
#> At iterate     1  f =       63.315  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 63.3155
#> 
#> F = 63.3155
#> final  value 63.315486 
#> converged
#> [mbo] 8: x=1.13 : y = 4.36 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -64.75254 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       64.753  |proj g|=      0.52075
#> At iterate     1  f =       63.962  |proj g|=       0.43422
#> At iterate     2  f =       63.285  |proj g|=       0.30913
#> At iterate     3  f =       63.269  |proj g|=        1.1478
#> At iterate     4  f =       63.261  |proj g|=      0.074181
#> At iterate     5  f =       63.261  |proj g|=     0.0026487
#> At iterate     6  f =       63.261  |proj g|=    6.4719e-06
#> 
#> iterations 6
#> function evaluations 9
#> segments explored during Cauchy searches 6
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 6.47193e-06
#> final function value 63.2613
#> 
#> F = 63.2613
#> final  value 63.261339 
#> converged
#> [mbo] 9: x=1.32 : y = 16 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -76.513 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       76.513  |proj g|=       1.3051
#> At iterate     1  f =       70.586  |proj g|=             0
#> 
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 70.586
#> 
#> F = 70.586
#> final  value 70.586034 
#> converged
#> [mbo] 10: x=1.41 : y = 20.4 : 0.0 secs : infill_ei
#> 
#> optimisation start
#> ------------------
#> * estimation method   : MLE 
#> * optimisation method : BFGS 
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model : 
#>   - type :  matern3_2 
#>   - nugget : NO
#>   - parameters lower bounds :  1e-10 
#>   - parameters upper bounds :  17.08579 
#>   - best initial criterion value(s) :  -85.50927 
#> 
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate     0  f=       85.509  |proj g|=       1.4973
#> At iterate     1  f =       82.144  |proj g|=        2.0383
#> ys=-2.885e+00  -gs= 2.242e+00, BFGS update SKIPPED
#> At iterate     2  f =       73.855  |proj g|=             0
#> 
#> iterations 2
#> function evaluations 3
#> segments explored during Cauchy searches 2
#> BFGS updates skipped 1
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 73.855
#> 
#> F = 73.855
#> final  value 73.854960 
#> converged
print(result)
#> Recommended parameters:
#> x=-1.96
#> Objective: y = 4.134
#> 
#> Optimization path
#> 10 + 10 entries in total, displaying last 10 (or less):
#>            x         y dob eol error.message exec.time         ei
#> 11 -2.050284  4.698625   1  NA          <NA>         0 -0.2288446
#> 12  3.623820 30.255372   2  NA          <NA>         0 -0.3726289
#> 13 -3.546339 32.155650   3  NA          <NA>         0 -0.3422541
#> 14  3.722633 25.569052   4  NA          <NA>         0 -0.3188126
#> 15 -3.787732 21.998262   5  NA          <NA>         0 -0.2604270
#> 16  1.938323  4.498631   6  NA          <NA>         0 -1.1183878
#> 17 -1.961884  4.134397   7  NA          <NA>         0 -0.9190494
#> 18  1.128445  4.357019   8  NA          <NA>         0 -0.4194018
#> 19  1.319269 15.956642   9  NA          <NA>         0 -1.4304011
#> 20  1.409904 20.427879  10  NA          <NA>         0 -0.4781482
#>    error.model constant.model train.time prop.type propose.time
#> 11        <NA>          FALSE      0.019 infill_ei        0.215
#> 12        <NA>          FALSE      0.016 infill_ei        0.216
#> 13        <NA>          FALSE      0.017 infill_ei        0.203
#> 14        <NA>          FALSE      0.018 infill_ei        0.218
#> 15        <NA>          FALSE      0.017 infill_ei        0.213
#> 16        <NA>          FALSE      0.019 infill_ei        0.253
#> 17        <NA>          FALSE      0.020 infill_ei        0.245
#> 18        <NA>          FALSE      0.016 infill_ei        0.249
#> 19        <NA>          FALSE      0.022 infill_ei        0.257
#> 20        <NA>          FALSE      0.017 infill_ei        0.232
#>    exec.timestamp        se      mean
#> 11     1476430742  8.576909 17.538842
#> 12     1476430743  8.972263 16.371550
#> 13     1476430743  9.408390 17.528535
#> 14     1476430744  9.843812 18.653697
#> 15     1476430744  9.651479 19.147651
#> 16     1476430745  5.725409  7.231362
#> 17     1476430745  4.622039  6.618227
#> 18     1476430746 10.029210 17.570495
#> 19     1476430747  7.264680  7.787929
#> 20     1476430747  9.935517 16.790109

Example run

There is also the function exampleRun, which is useful to figure out how mbo works and to visualize the results.

ex = exampleRun(obj.fun, control = control, show.info = FALSE)
#> Warning in makeMboLearner(control, fun): filter.proposed.points is not set
#> in the control object. This might lead to the 'leading minor of order ...'
#> error during model fit.
print(ex)
#> MBOExampleRun
#> Number of parameters        : 1
#> Parameter names             : x
#> Parameter types             : numericvector
#> Global Opt (known)          : 0.0000e+00
#> Gap for best point          : 9.9628e-01
#> True points per dim.        : 50
#> Objectives                    : 1
#> Points proposed per iter      : 1
#> 
#> Infill criterion              : ei
#> Infill optimizer              : focussearch
#> Infill optimizer restarts     : 1
#> Final point by                : best.true.y
#> Learner                     : regr.km
#> Learner settings:
#> jitter=FALSE,nugget.stability=1e-08
#> Recommended parameters:
#> x=-0.992
#> Objective: y = 9.963e-01
plotExampleRun(ex)

plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5

Or alternatively for a two dimensional function:

obj.fun2 = makeRastriginFunction(2L)
design2 = generateDesign(n = 10, par.set = getParamSet(obj.fun2))
ex2 = exampleRun(obj.fun2, control = control, show.info = FALSE)
#> Warning in makeMboLearner(control, fun): filter.proposed.points is not set
#> in the control object. This might lead to the 'leading minor of order ...'
#> error during model fit.
print(ex2)
#> MBOExampleRun
#> Number of parameters        : 2
#> Parameter names             : x1,x2
#> Parameter types             : numericvector
#> Global Opt (known)          : 0.0000e+00
#> Gap for best point          : 1.8144e+01
#> True points per dim.        : 50
#> Objectives                    : 1
#> Points proposed per iter      : 1
#> 
#> Infill criterion              : ei
#> Infill optimizer              : focussearch
#> Infill optimizer restarts     : 1
#> Final point by                : best.true.y
#> Learner                     : regr.km
#> Learner settings:
#> jitter=FALSE,nugget.stability=1e-08
#> Recommended parameters:
#> x=-1.84,1.8
#> Objective: y = 1.814e+01
plotExampleRun(ex2)
#> Loading required package: gridExtra

plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal
#> Warning: Not possible to generate contour data
#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal
#> Warning: Not possible to generate contour data

plot of chunk unnamed-chunk-7

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : Not possible to generate contour data
#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal
#> Warning: Not possible to generate contour data

plot of chunk unnamed-chunk-7

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : Not possible to generate contour data
#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal
#> Warning: Not possible to generate contour data

plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal

#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : Not possible to generate contour data
#> Warning in grDevices::contourLines(x = sort(unique(data$x)), y =
#> sort(unique(data$y)), : all z values are equal
#> Warning: Not possible to generate contour data

plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7plot of chunk unnamed-chunk-7